﻿using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using NetBay.Core.BusinessEntities;
using NetBay.Core.BusinessEntities.AdvancedSearchCriteria;

namespace Rio.Framework.Pages.CriteriaControls
{
    public partial class StackIndexSearch : UserControl
    {
        #region Attributs

        private Organization _organization;
        private FolderType _folderType;

        #endregion

        #region Properties

        public Organization CurrentOrganization
        {
            get { return _organization; }
        }

        public FolderType CurrentFolderType
        {
            get { return _folderType; }
        }

        #endregion

        #region Constructors

        public StackIndexSearch()
        {
            InitializeComponent();
        }

        public StackIndexSearch(Organization organization, FolderType folderType)
            : this()
        {
            _organization = organization;
            _folderType = folderType;

            Initialize(organization, folderType);
        }

        #endregion

        #region Private functions

        /// <summary>
        /// Scroll to the control
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void L_oUIElementIndexControl_GotFocus(object sender, RoutedEventArgs e)
        {
            if (sender is FrameworkElement)
            {
                scrollViewerSearch.ScrollIntoView(sender as FrameworkElement);
            }
        }

        #endregion

        #region Public functions

        /// <summary>
        /// Initialize control
        /// </summary>
        public void Initialize(Organization organization, FolderType folderType)
        {
            _organization = organization;
            _folderType = folderType;

            this.Clear();

            if (_organization != null && _folderType != null)
            {
                //Add each controls on the panel
                foreach (Index ind in _folderType.LstIndex)
                {
                    if (ind.IsVisible)
                    {
                        AdvancedSearchControl control = new AdvancedSearchControl(ind, _organization.Identifier,
                                                                                  _folderType.Identifier);
                        StackControl.Children.Add(control);
                    }
                }

                foreach (UIElement L_oUIElementIndexControl in StackControl.Children)
                {
                    L_oUIElementIndexControl.GotFocus += L_oUIElementIndexControl_GotFocus;
                }
            }
        }

        /// <summary>
        /// Returns All inputed values 
        /// </summary>
        /// <returns></returns>
        public List<AdvancedSearchCriteriaItem> GetAllValues()
        {
            List<AdvancedSearchCriteriaItem> result = new List<AdvancedSearchCriteriaItem>();
            foreach (AdvancedSearchControl item in StackControl.Children)
            {
                AdvancedSearchCriteriaItem ad = item.GetCriterias();
                if (ad != null && ad.CriteriaValue != null && ad.CriteriaValue.Count() > 0)
                    result.Add(ad);
            }
            return result;
        }


        /// <summary>
        /// Clear values
        /// </summary>
        public void ClearCriterias()
        {
            foreach (AdvancedSearchControl item in StackControl.Children)
            {
                //item.ClearCriterias();
                item.Initialize();
            }
        }

        /// <summary>
        /// Remove all controls
        /// </summary>
        public void Clear()
        {
            StackControl.Children.Clear();
        }

        #endregion

    }
}
